package com.cloudkinto.appletservice.fbaproduct.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.appletservice.fbaproduct.AppletFbaProductService;
import com.cloudkinto.appletservice.fbaproduct.vo.*;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.exception.BizExceptionI18;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.dao.*;
import com.cloudkinto.entity.*;
import com.cloudkinto.service.file.FileService;
import com.cloudkinto.service.operate.vo.OperateRes;
import com.cloudkinto.service.orderoutput.OrderOutputService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author zhangyy
 * @date 2020/8/20 9:04
 * @description:
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class AppletFbaProductServiceImpl extends ServiceImpl<ProductInfoDao, ProductInfoDo> implements AppletFbaProductService {
    @Autowired
    private ProductInfoDao dao;
    @Autowired
    private DicBusinessItemDao dicBusinessItemDao;
    @Autowired
    private ProductSkuMapDao productSkuMapDao;
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private FileService fileService;
    @Autowired
    private FileDao fileDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private OrderOutputService orderOutputService;
    @Autowired
    private FbaStorageDao fbaStorageDao;


    @Override
    public AppletFbaProductDetailRes detail(Long id) {
        ProductInfoDo productInfoDo = dao.selectById(id);
        if (productInfoDo == null) throw new BizExceptionI18("no.product.info");
        AppletFbaProductDetailRes res = new AppletFbaProductDetailRes();
        BeanUtils.copyProperties(productInfoDo, res);
        res.setProductPictureFileUrl(fileService.getFilePath(productInfoDo.getProductPictureFileId()));
        //操作日志
//        List<OperateRes> operateResList = new ArrayList<>();
//        LambdaQueryWrapper<OperateRecordDo> operateRecordDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        operateRecordDoLambdaQueryWrapper.eq(OperateRecordDo::getThirdId, id).eq(OperateRecordDo::getType, 1).orderByDesc(OperateRecordDo::getId);
//        List<OperateRecordDo> operateRecordDos = operateRecordDao.selectList(operateRecordDoLambdaQueryWrapper);
//        if (!CollectionUtils.isEmpty(operateRecordDos)) {
//            for (OperateRecordDo operateRecordDo : operateRecordDos) {
//                OperateRes operateRes = new OperateRes();
//                operateRes.setContent(operateRecordDo.getContent());
//                operateRes.setOperateTime(operateRecordDo.getOperateTime());
//                UserDo userDo = userDao.selectById(operateRecordDo.getOperateBy());
//                if (userDo != null) {
//                    operateRes.setOperateBy(userDo.getUserName());
//                }
//                operateResList.add(operateRes);
//            }
//        }
//        res.setOperateResList(operateResList);
        return res;
    }


    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<ProductInfoDo> wrapper = queryBuild(map);
        IPage<ProductInfoDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<AppletFbaProductListPage> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    private IPage<ProductInfoDo> pageInit(Map<String, String> map) {
        IPage<ProductInfoDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 100);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    //查询分页列表之后对列表数据的处理
    private List<AppletFbaProductListPage> handlerListAfter(List<ProductInfoDo> list) {
        List<AppletFbaProductListPage> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (ProductInfoDo infoDo : list) {
                AppletFbaProductListPage res = new AppletFbaProductListPage();
                BeanUtils.copyProperties(infoDo, res);
                res.setProductPictureFileUrl(fileService.getFilePath(infoDo.getProductPictureFileId()));
                result.add(res);
            }
        }
        return result;
    }

    private QueryWrapper<ProductInfoDo> queryBuild(Map<String, String> map) {
        QueryWrapper<ProductInfoDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("keyword"))) {
            wrapper.and(i -> i.like("commodity_sku", map.get("keyword"))
                    .or().like("storage_sku", map.get("keyword"))
                    .or().like("commodity_name", map.get("keyword"))
                    .or().like("product_name", map.get("keyword")));
        }
        if (StringUtils.isNotBlank(map.get("productType"))) {
            wrapper.like("product_type", map.get("productType"));
        }
        if (StringUtils.isNotBlank(map.get("companyId"))) {
            wrapper.eq("company_id", map.get("companyId"));
        }
        wrapper.eq("type", 2);
        return wrapper.orderByDesc("id");
    }


}
